﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Data;
//using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Xml;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace ChangeContentTypeName
{
    class Program
    {
        static void Main(string[] args)
        {
            string strSiteUrl = string.Empty;
            string strContentTypeOld = string.Empty;
            string strContentTypeNew = string.Empty;

            strSiteUrl = args[0];
            strContentTypeOld = args[1];
            strContentTypeNew = args[2];

            using (SPSite objSPSite = new SPSite(strSiteUrl))
            {
                using (SPWeb objSPWeb = objSPSite.OpenWeb(objSPSite.RootWeb.ID))
                //using (SPWeb objSPWeb = objSPSite.OpenWeb("IT"))
                {
                    objSPWeb.AllowUnsafeUpdates = true;
                    SPListCollection objSPListCollection = objSPWeb.Lists;
                    foreach (SPList objSPList in objSPListCollection)
                    {
                        if (objSPList.BaseType == SPBaseType.DocumentLibrary)
                        {
                            SPListItemCollection objFolders = objSPList.Folders;
                            Console.WriteLine(string.Format("DocumentLibrary={0}, ContentType Old={1}, ContentType New={2}", objSPList.Title, strContentTypeNew, strContentTypeOld));
                            foreach (SPContentType objSPContentType in objSPList.ContentTypes)
                            {
                                if (objSPContentType.Name.Equals(strContentTypeOld, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    objSPContentType.ReadOnly = false;
                                    //objSPContentType.Name = strContentTypeNew;
                                    //objSPContentType.UpdateIncludingSealedAndReadOnly(true);
                                    objSPContentType.Update();
                                    //objSPContentType.ReadOnly = true;
                                    //objSPContentType.Update();
                                }
                                else if (objSPContentType.Name.Equals(strContentTypeNew, StringComparison.InvariantCultureIgnoreCase) && objSPContentType.ReadOnly == false)
                                {
                                    objSPContentType.ReadOnly = false;
                                    objSPContentType.Update();
                                }
                            }
                        }
                    }
                    objSPWeb.AllowUnsafeUpdates = false;
                }
            }
        }
    }
}
